/*
 *  tf_renderer.h
 *  Perceel
 *
 *  Created by Nicola Gigante on 20/03/08.
 *  Copyright 2008 Nicola Gigante. All rights reserved.
 *
 */

#ifndef __TF_RENDERER_H__
#define __TF_RENDERER_H__

#include <QWidget>
#include <QMouseEvent>
#include <QPicture>
#include <QPainter>
#include <QStyleOptionViewItem>
#include <QMessageBox>
#include <QtDebug>

#include "tf.h"

#define BORDER 10
#define FRACTLINE 10

/*
 * The transfer function can be drawn only with roots and poles in evidence.
 *
 * The method drawRoot() paints roots and poles, including the brackets and
 * the exponent. It returns a QPicture with the root drawn on it.
 * A drawRoot method is called for each root and each pole in the TF in
 * the paintTF() method. Then all the QPicture's are painted on one big 
 * QPicture that is returned by the method. This method is called by the
 * widget's paintEvent() method that actually paints the QPicture over the
 * screen, or by the list view delegate from the main window.
 * By the selection handling point of view, an internal variable selectionPoint
 * is updated every time a click event is received from the widget.
 * In this way, the paintTF() can detect which root or pole the point clicked 
 * belongs to, and then it can paint of a different color the background of the
 * corresponding area.
 */
class TFRenderer : public QWidget
{
	Q_OBJECT
	
	TF m_tf;
	
	int m_selectedRoot;
	int m_selectedPole;
	QList<QRect> rootsAreas;
	QList<QRect> polesAreas;
	
	int m_border;
	
public:
	TFRenderer(QWidget *parent = NULL);
	TFRenderer(const QStyleOptionViewItem &item);
	
	// Proprietà
	TF tf() const { return m_tf; }
	void setTF(TF tf);
	
	QPicture paintTF(QList<QRect> *rootsAreas = NULL, QList<QRect> *polesAreas = NULL) const;
	virtual QSize sizeHint() const;
	
	int renderBorder() const { return m_border; }
	void setRenderBorder(int b) { m_border = b; update(); }
	
	int selectedRoot() const { return m_selectedRoot; }
	void setSelectedRoot(int s);
	
	int selectedPole() const { return m_selectedPole; }
	void setSelectedPole(int s);

signals:
	void selectionChanged();
	
protected:
	virtual void paintEvent(QPaintEvent *event);
	virtual void mousePressEvent(QMouseEvent *event);
	virtual void mouseMoveEvent(QMouseEvent *event);
	
private:
	QPicture drawRoot(const Complex &root, int exponent, QPointF start, Qt::Alignment align = Qt::AlignBottom) const;
};

#endif
